package statadminlogic

import (
	"context"
	"github.com/shopspring/decimal"
	"go.uber.org/zap"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"strconv"
	"time"

	"navi-mumbai/app/rpc/stat/internal/svc"
	"navi-mumbai/app/rpc/stat/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryGameOrderStatLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryGameOrderStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryGameOrderStatLogic {
	return &QueryGameOrderStatLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询游戏订单统计
func (l *QueryGameOrderStatLogic) QueryGameOrderStat(in *pb.QueryGameOrderStatReq) (*pb.QueryGameOrderStatResp, error) {
	defer utils.RecoverPanic()

	var start, end int64
	if in.StartTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.StartTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		start, _ = strconv.ParseInt(t.Format(dbkey.DateFormat2), 10, 64)
	}
	if in.EndTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.EndTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		end, _ = strconv.ParseInt(t.Format(dbkey.DateFormat2), 10, 64)
	}

	var resp pb.QueryGameOrderStatResp
	var err error
	var tmp int64
	tmp, err = l.svcCtx.UserGameDailyOrderStatModel.SumOrderAmount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	resp.OrderAmount = decimal.NewFromInt(tmp).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)

	resp.OrderCount, err = l.svcCtx.UserGameDailyOrderStatModel.SumOrderCount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	resp.OrderUserCount, err = l.svcCtx.UserGameDailyOrderStatModel.SumOrderUserCount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	tmp, err = l.svcCtx.UserGameDailyOrderStatModel.SumOrderReturnAmount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	resp.ReturnAmount = decimal.NewFromInt(tmp).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)

	resp.ReturnCount, err = l.svcCtx.UserGameDailyOrderStatModel.SumOrderReturnCount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	resp.ReturnUserCount, err = l.svcCtx.UserGameDailyOrderStatModel.SumOrderReturnUserCount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return &resp, nil
}
